package com.atu.rtc.factory;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import org.java_websocket.client.WebSocketClient;
import org.webrtc.Camera1Enumerator;
import org.webrtc.Camera2Enumerator;
import org.webrtc.CameraEnumerator;
import org.webrtc.IceCandidate;
import org.webrtc.SessionDescription;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoCapturer;

public class CameraCapturer extends AbstractCapturer implements ICapturer {
    private Activity activity;
    private WebSocketClient client;
    private View cover;

    public CameraCapturer(Activity activity, View cover) {
        this.activity = activity;
        this.cover = cover;
    }

    @Override
    public void init(WebSocketClient client,SurfaceViewRenderer surfaceViewRenderer) {
        this.client = client;
        this.localSurfaceView = surfaceViewRenderer;
        createPeerConnection(localSurfaceView);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                cover.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void setIntent(Intent intent) {

    }

    @Override
    public void connect() {

    }

    @Override
    public void setRemoteDescription(SessionDescription sessionDescription) {
        peerConnection.setRemoteDescription(observer, sessionDescription);
    }

    @Override
    public void addIceCandidate(IceCandidate iceCandidate) {
        peerConnection.addIceCandidate(iceCandidate);
    }

    @Override
    public void createAnswer() {
        super.createAnswer(peerConnection);
    }

    @Override
    public void createOffer() {
        super.createOffer(peerConnection);
    }

    @Override
    protected int getWidth() {
        return 320;
    }

    @Override
    protected int getHeight() {
        return 240;
    }

    @Override
    protected Activity getActivity() {
        return activity;
    }


    private VideoCapturer createCameraCapturer(CameraEnumerator enumerator) {
        final String[] deviceNames = enumerator.getDeviceNames();

        // First, try to find front facing camera
        for (String deviceName : deviceNames) {
            if (enumerator.isFrontFacing(deviceName)) {
                VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
                if (videoCapturer != null) {
                    return videoCapturer;
                }
            }
        }

        // Front facing camera not found, try something else
        for (String deviceName : deviceNames) {
            if (!enumerator.isFrontFacing(deviceName)) {
                VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
                if (videoCapturer != null) {
                    return videoCapturer;
                }
            }
        }
        return null;
    }

    @Override
    public void close() {
        super.close();
    }

    @Override
    protected VideoCapturer createVideoCapturer() {
        if (Camera2Enumerator.isSupported(getActivity())) {
            return createCameraCapturer(new Camera2Enumerator(getActivity()));
        } else {
            return createCameraCapturer(new Camera1Enumerator(false));
        }
    }

    @Override
    protected WebSocketClient getClient() {
        return client;
    }
}
